package demo.java.util.stream;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Log {

    // The number of characters that may be read.
    private static final int READ_AHEAD_LIMIT = 100_000_000;

    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\hanjy\\Desktop\\mongo_147.log";
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            testACCESS(reader);
        } catch (FileNotFoundException e) {
            System.err.println(e);
        }
    }
    
    static void testACCESS(BufferedReader reader) {
        Map<String, Long> map = reader.lines().filter(line -> {
            return line.indexOf(" ACCESS  ") > 0;
        }).peek(System.out::println).map(line -> {
            String tmp = line.substring(76, 89);
            return tmp;
        }).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(map);
    }

    /**
     * 查询IP及其次数
     * 
     * @param reader
     */
    static void testIpAndCount(BufferedReader reader) {
        Map<String, Long> map = reader.lines().filter(line -> {
            return line.indexOf(" NETWORK  ") > 0;
        }).peek(System.out::println).map(line -> {
            String tmp = line.substring(75, 89);
            System.out.println(tmp);
            return tmp;
        }).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
        System.out.println(map);
    }

}
